package net.opentsdb.tsd;

import cn.hutool.core.lang.TypeReference;
import net.opentsdb.core.IncomingDataPoint;
import net.opentsdb.core.TSDB;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelFutureListener;
import org.jboss.netty.handler.codec.http.HttpHeaders;
import org.jboss.netty.handler.codec.http.HttpMethod;
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.jboss.netty.handler.codec.http.HttpResponseStatus;
import org.jboss.netty.util.CharsetUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Hujf
 * @title: HttpQuery
 * @date 2021-09-22 16:06
 * @description: http 请求
 */
public class HttpQuery extends AbstractHttpQuery{

    private Logger LOG = LoggerFactory.getLogger(HttpQuery.class);
    /** The serializer to use for parsing input and responding */
    private HttpSerializer serializer = null;

    public HttpQuery(TSDB tsdb, HttpRequest request, Channel channel) {
        super(tsdb,request,channel);
        this.serializer = new HttpJsonSerializer(this);
    }

    @Override
    public String getQueryBaseRoute() {

        return null;
    }

    public HttpSerializer serializer() {
        return this.serializer;
    }

    @Override
    public void notFound() {
        LOG.warn("Not Found: " + request().getUri());
        sendReply(HttpResponseStatus.NOT_FOUND, PAGE_NOT_FOUND);
    }


    public void sendReply(final HttpResponseStatus status ,final StringBuilder buf) {
        sendBuffer(status,ChannelBuffers.copiedBuffer(buf.toString(),CharsetUtil.UTF_8));
    }

    public void sendReply(final ChannelBuffer buf) {
        sendBuffer(HttpResponseStatus.OK,buf);
    }

    public void sendReply(final byte[] data) {
        sendBuffer(HttpResponseStatus.OK,ChannelBuffers.wrappedBuffer(data));
    }



    /**
     * Sends an HTTP reply to the client.
     * @param status The status of the request (e.g. 200 OK or 404 Not Found).
     * @param buf The content of the reply to send.
     */
    private void sendBuffer(final HttpResponseStatus status,
                            final ChannelBuffer buf) {
        final String contentType =
                serializer.responseContentType();
        sendBuffer(status, buf, contentType);
    }



    /** Precomputed 404 page. */
    private static final StringBuilder PAGE_NOT_FOUND =
            makePage("Page Not Found", "Error 404",
                    "<blockquote>"
                            + "<h1>Page Not Found</h1>"
                            + "The requested URL was not found on this server."
                            + "</blockquote>");

    private static StringBuilder makePage(String s1, String s2, String s3) {
        StringBuilder builder = new StringBuilder();
        builder.append(s1);
        return builder;
    }

    public String[] explodeAPIPath() {
        String[] split = null;

        return split;
    }
}
